package com.woniu.bh.beautysalon.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.bh.beautysalon.model.dto.AllServiceDto;
import com.woniu.bh.beautysalon.model.dto.ServiceRankDto;
import com.woniu.bh.beautysalon.model.params.NewServiceParam;
import com.woniu.bh.beautysalon.model.params.PageParam;
import com.woniu.bh.beautysalon.web.service.IBeautysalonServiceService;
import com.woniu.bh.common.core.dto.ResultDto;
import com.woniu.bh.common.core.exception.ProjectException;
import com.woniu.bh.common.core.exception.ProjectExceptionResult;
import com.woniu.bh.common.core.util.Constants;
import com.woniu.bh.common.core.util.JwtUtil;
import com.woniu.bh.common.core.util.UploadHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName BeautysalonServiceController
 * @createTime 2020年10月15日 11:26:00
 */
@RestController
@RequestMapping("/product")
@Slf4j
@Api(value = "美容馆接口", tags = "美容馆服务管理")
public class BeautysalonServiceController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IBeautysalonServiceService bss;

    /**
     * @description: 服务排行
     * @param:
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("/hotService")
    @ApiOperation("我的热门服务")
    public ResultDto hotService(HttpServletRequest request) {
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            ServiceRankDto serviceRankDto = bss.serviceRank(beautysalonId);
            return ResultDto.builder().success(true).code(1).message("查询成功").object(serviceRankDto).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }


    /**
     * @description: 查看美容馆所有服务
     * @param: pageParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("/getAllServices")
    @ApiOperation("查看我所有的服务")
    public ResultDto getAllServices(PageParam pageParam, HttpServletRequest request) {
        log.info("传入参数:{}", pageParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            IPage<AllServiceDto> iPage = bss.getAllService(pageParam.getPageIndex(), pageParam.getPageSize(), beautysalonId);
            return ResultDto.builder().success(true).code(1).message("查询成功").object(iPage).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 新增服务
     * @param: newServiceParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/addNewService")
    @ApiOperation("新增服务")
    public ResultDto addNewService(NewServiceParam newServiceParam, HttpServletRequest request) {
        log.info("传入参数:{}", newServiceParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            int serviceId = bss.addNewService(newServiceParam, beautysalonId);
            if (serviceId != 0) {
                return ResultDto.builder().success(true).code(1).message("新增成功").object(serviceId).build();
            }
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 上传图片
     * @param: file
     * @param: request
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/servicePic")
    @ApiOperation("上传服务图片")
    public ResultDto servicePic(@RequestParam("file3") MultipartFile file, HttpServletRequest request) {
        //解析token获取id
        String token = request.getHeader("X-Token");
        Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());

        UploadHandler uploadHandler = new UploadHandler();
        try {
            String fileUrl = uploadHandler.upload(Constants.BEAUTYSALON_SERVICE, file, beautysalonId + "");
            stringRedisTemplate.opsForValue().set("servicePic", fileUrl);
            return ResultDto.builder().success(true).code(1).message("上传成功").build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 下架
     * @param: serviceId
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/downService/{serviceId}")
    @ApiOperation("下架服务")
    public ResultDto downService(@PathVariable Integer serviceId) {
        log.info("传入参数:{}", serviceId);
        try {
            int result = bss.downService(serviceId);
            if (result > 0) {
                return ResultDto.builder().success(true).code(1).message("下架成功").object(result).build();
            }
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

}

